<!--
 * @Description:
 * @Author: zhaolei
 * @Date: 2023-04-13 11:21:48
-->
<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @search-change="searchChange"
      @search-reset="searchReset"
    >
      <template #closeRemark="{ row }">
        <el-tooltip
          effect="dark"
          :content="row.closeRemark"
          placement="top-start"
        >
          <span class="closeRemark">{{ row.closeRemark }}</span>
        </el-tooltip>
      </template>
      <template
        slot="menu"
        slot-scope="scope"
      >
        <el-button
          v-if="scope.row.status === SIGN_STATUS.TO_BE_CONFIRMED"
          type="text"
          size="small"
          @click="confirm(scope.row, scope.index)"
        >
          确认预约
        </el-button>
        <el-button
          v-if="scope.row.status === SIGN_STATUS.TO_BE_CONFIRMED"
          type="text"
          size="small"
          @click="close(scope.row, scope.index)"
        >
          关闭
        </el-button>
        <el-button
          v-if="scope.row.reserveType === RESERVE_TYPE.PLACE_OUT_ADOPT"
          type="text"
          size="small"
          @click="petinformation(scope.row)"
        >
          猫咪信息
        </el-button>
      </template>
    </avue-crud>
    <confirmPopup
      :id="id"
      v-model="visible"
      @success="(val) => getList(page, val)"
    ></confirmPopup>
    <closePopup
      :id="id"
      v-model="closeVisible"
      @success="(val) => getList(page, val)"
    ></closePopup>
    <petPopup
      :id="id"
      v-model="petVisible"
      @success="(val) => getList(page, val)"
    ></petPopup>
  </basic-container>
</template>

<script>
import { fetchList, getObj } from '@/api/core/appointment';
import { tableOption } from '@/const/crud/appointment/record';
import { mapGetters } from 'vuex';
import crudMixins from '@/mixins/crud';
import { SIGN_STATUS, RESERVE_TYPE } from '@/const/type';
import confirmPopup from './components/confirmPopup.vue';
import closePopup from './components/closePopup.vue';
import petPopup from './components/petPopup.vue';
export default {
  name: 'appointmentRecord',
  components: {
    confirmPopup,
    closePopup,
    petPopup,
  },
  mixins: [crudMixins],
  data() {
    return {
      form: {},
      tableOption: tableOption(),
      SIGN_STATUS,
      RESERVE_TYPE,
      visible: false,
      petVisible: false,
      id: null,
      closeVisible: false,
    };
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  watch: {},
  methods: {
    // 列表查询
    getList(page, refresh) {
      if (refresh) {
        this.page.current = 1;
        this.page.currentPage = 1;
      }
      this.tableLoading = true;
      const timeObj = {};
      if (this.searchForm.reserveTime && this.searchForm.reserveTime.length) {
        timeObj.reserveStartDate = this.searchForm.reserveTime[0];
        timeObj.reserveEndDate = this.searchForm.reserveTime[1];
        // delete this.searchForm.reserveTime;
      }
      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize,
          },
          this.searchForm,
          timeObj
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
          this.tableData.forEach((el) => {
            el.petCode = el.petCode || '无';
            if (el.petCode && el.petName) {
              el.petName = `${el.petName}（${el.petCode}）`;
            } else {
              el.petName = '无';
            }
          });
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    confirm(row) {
      this.id = row.id;
      this.visible = true;
    },
    close(row) {
      this.id = row.id;
      this.closeVisible = true;
    },
    //猫咪信息
    petinformation(row) {
      this.petVisible = true;
      this.id = row.id;
    },
    async beforeOpen(done, btnType) {
      // 查看
      if (btnType !== 'add') {
        const { data } = await getObj(this.form.id);
        this.form = data.data;
      }
      done();
    },
  },
};
</script>
<style lang="scss" scoped>
.closeRemark {
  width: 100px !important;
  overflow: hidden; /* 超出一行文字自动隐藏 */
  text-overflow: ellipsis; /* 文字隐藏后添加省略号 */
  white-space: nowrap; /* 强制不换行 */
}
::v-deep {
  .avue-group .el-collapse-item__header {
    margin-bottom: 0;
  }
  // .el-textarea.el-input--small.is-disabled.avue-input .el-textarea__inner {
  //   height: 30px !important;
  //   min-height: 30px !important;
  // }
}
</style>
